package com.liuhouse.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.liuhouse.enums.AppHttpCodeEnum;

import java.io.Serializable;

/**
 * 统一响应格式,实体类，或者这个类严格来说叫做响应类
 * T 返回的数据实体类的类型  泛型
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult <T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    //无参构造
    public ResponseResult(){
        this.code= AppHttpCodeEnum.SUCCESS.getCode();
        this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
    }

    public ResponseResult(Integer code , T data){
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code , String msg , T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(Integer code , String msg){
        this.code = code;
        this.msg = msg;
    }

    public static ResponseResult errorResult(int code , String msg){
        ResponseResult result = new ResponseResult();
        return result.error(code , msg);
    }

    public static ResponseResult okResult(){
        ResponseResult responseResult = new ResponseResult();
        return responseResult;
    }

    public static ResponseResult okResult(int code , String msg){
        ResponseResult responseResult = new ResponseResult();
        return responseResult.ok(code , null , msg);
    }

    public static ResponseResult okResult(Object data){
        ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS , AppHttpCodeEnum.SUCCESS.getMsg());
        if(data != null){
            result.setData(data);
        }
        return result;
    }

    public static ResponseResult errorResult(AppHttpCodeEnum enums){
        return setAppHttpCodeEnum(enums, enums.getMsg());
    }

    public static ResponseResult errorResult(AppHttpCodeEnum enums , String msg){
        return setAppHttpCodeEnum(enums , msg);
    }

    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){
        return okResult(enums.getCode() , enums.getMsg());    }

    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums , String msg){
        return okResult(enums.getCode() , msg);
    }



    public ResponseResult<?> error(Integer code , String msg){
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(Integer code , T data){
        this.code = code;
        this.data = data;
        return this;
    }

    public ResponseResult<?> ok(Integer code , T data , String msg){
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(T data){
        this.data = data;
        return this;
    }


    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}
